package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.DoubleLeftAdapter;
import zj.com.adapter.JcrRightAdapter;
import zj.com.model.Org;
import zj.com.model.User;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/21.
 *
 */
public class JcrSelectDoubleListActivity extends  BaseActivity implements View.OnClickListener{

    public TextView title_name,right_tv;
    public ImageView left_iv;

    public ListView left_listView,right_listView;

    public DoubleLeftAdapter leftAdapter;
    public JcrRightAdapter rightAdapter;

    public List<Org> leftList;
    public List<User> rightList;

    public boolean isDobule = false;
    public Org selectOrg;

    public RelativeLayout error_rl;
    public LinearLayout content_ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_double);

        initView();
    }

    public void initView(){


        error_rl = (RelativeLayout) findViewById(R.id.error_rl);
        content_ll = (LinearLayout) findViewById(R.id.content_ll);
        isDobule = getIntent().getBooleanExtra("isDouble",false);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("选择");
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);

        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("确定");
        right_tv.setVisibility(View.GONE);
        right_tv.setOnClickListener(this);

        if(isDobule){
            right_tv.setVisibility(View.VISIBLE);

        }


        left_listView = (ListView) findViewById(R.id.left_listView);
        right_listView = (ListView) findViewById(R.id.right_listView);
        leftList =new ArrayList<>();


        rightList = new ArrayList<>();


        leftAdapter = new DoubleLeftAdapter(leftList,this);
        left_listView.setAdapter(leftAdapter);
        rightAdapter = new JcrRightAdapter(rightList,this);
        right_listView.setAdapter(rightAdapter);




        left_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               Org selectOrg = leftList.get(position);
                leftAdapter.setSelectPosition(position);
                obtainJcr(selectOrg);

            }
        });

        right_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                User user =   rightList.get(position);
                Intent intent  = new Intent();
                intent.putExtra("user",user);
                setResult(RESULT_OK,intent);
                finish();

            }
        });
        obtainBm();

    }

    public ProgressDialog dialog;


    public void obtainBm(){
        dialog = DialogUtil.createProgressDialog(this,"提示","数据获取中...");
        NetManager.getInstance(this).QueryAllOrgs(new OnResponseListener<List<Org>>() {
            @Override
            public void onComplete(boolean isOK, List<Org> result, int errorCode, String errorMsg) {

                if(isOK){
                    leftList = result;
                    if(leftList != null && leftList.size() > 0){
                        leftAdapter.setDataChange(leftList);
                        leftAdapter.setSelectPosition(0);
                        obtainJcr(leftList.get(0));
                        error_rl.setVisibility(View.GONE);
                        content_ll.setVisibility(View.VISIBLE);
                    }else{
                        error_rl.setVisibility(View.VISIBLE);
                        content_ll.setVisibility(View.GONE);


                        dialog.dismiss();
                    }
                }else{
                    dialog.dismiss();
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();

                Toast.makeText(JcrSelectDoubleListActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void obtainJcr(Org org){

        NetManager.getInstance(this).GetUserListByKeyWord("","Inspect",org.getOrgCode() + "", new OnResponseListener<List<User>>() {
            @Override
            public void onComplete(boolean isOK, List<User> result, int errorCode, String errorMsg) {

                if(isOK){

                    if(result != null && result.size() > 0){

                        rightList = result;
                        rightAdapter.setDataChange(rightList);

                    }else{
                        rightList.clear();
                        rightAdapter.setDataChange(rightList);
                    }
                }else{
                    Toast.makeText(JcrSelectDoubleListActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                }
                dialog.dismiss();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                Toast.makeText(JcrSelectDoubleListActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:

                break;
        }
    }
}
